package org.helldorado.companybuilder;

import java.util.HashMap;
import java.util.Map;

import org.helldorado.companybuilder.R;
import org.xmlpull.v1.XmlPullParser;

import android.content.Context;
import android.util.Log;

public class UnitNature {
	private static UnitNature s_unitNatures = null;
	private Map<Integer, String> m_factions = null;
	private Map<Integer, String> m_ranks = null;
	private Map<Integer, String> m_genders = null;
	private Map<Integer, String> m_species = null;
	private Map<Integer, String> m_infernalists = null;
	private Map<Integer, String> m_religions = null;
	
	
	public static UnitNature getInstance() {
		if (s_unitNatures == null) {
			s_unitNatures = new UnitNature();
		}
		return s_unitNatures;
	}
	
	public static String getFactionById(Integer id) {	
		String ret = getInstance().m_factions.get(id);
		if (ret == null){
			ret = "Faction unknown";
		}
		return ret;
	}
	
	public static String getRankById(Integer id) {	
		String ret = getInstance().m_ranks.get(id);
		if (ret == null){
			ret = "Rank unknown";
		}
		return ret;
	}
	
	public static String getGenderById(Integer id) {	
		String ret = getInstance().m_genders.get(id);
		if (ret == null){
			ret = "Gender unknown";
		}
		return ret;
	}
	
	public static String getSpeciesById(Integer id) {	
		String ret = getInstance().m_species.get(id);
		if (ret == null){
			ret = "Species unknown";
		}
		return ret;
	}
	
	public static String getInfernalistById(Integer id) {	
		String ret = getInstance().m_infernalists.get(id);
		if (ret == null){
			ret = "Infernalist type unknown";
		}
		return ret;
	}

	public static String getReligionById(Integer id) {	
		String ret = getInstance().m_religions.get(id);
		if (ret == null){
			ret = "Religion unknown";
		}
		return ret;
	}

	private UnitNature()
	{
		m_factions = new HashMap<Integer, String>();
		m_ranks = new HashMap<Integer, String>();
		m_genders = new HashMap<Integer, String>();
		m_species = new HashMap<Integer, String>();
		m_infernalists = new HashMap<Integer, String>();
		m_religions = new HashMap<Integer, String>();
		try {
			XmlPullParser xpp0= MainActivity.getContext().getResources().getXml(R.xml.hd_general);
			
			int eventType0 = xpp0.getEventType();

			// Loop through pull events until we 4reach END_DOCUMENT
			while (eventType0 != XmlPullParser.END_DOCUMENT) {
				String tagname0 = xpp0.getName();

				// React to different event types appropriately
				switch (eventType0) {
				case XmlPullParser.START_TAG:
					if (tagname0.equalsIgnoreCase("faction")) {
						String title = xpp0.getAttributeValue(null, "title");
						Integer id = Integer.parseInt(xpp0.getAttributeValue(null, "id"));
						m_factions.put(id, title);
					}
					if (tagname0.equalsIgnoreCase("rank")) {
						String title = xpp0.getAttributeValue(null, "title");
						Integer id = Integer.parseInt(xpp0.getAttributeValue(null, "id"));
						m_ranks.put(id, title);
					}
					if (tagname0.equalsIgnoreCase("gender")) {
						String title = xpp0.getAttributeValue(null, "title");
						Integer id = Integer.parseInt(xpp0.getAttributeValue(null, "id"));
						m_genders.put(id, title);
					}
					if (tagname0.equals("species")) {
						String title = xpp0.getAttributeValue(null, "title");
						Integer id = Integer.parseInt(xpp0.getAttributeValue(null, "id"));
						m_species.put(id, title);
					}
					if (tagname0.equalsIgnoreCase("infernalist")) {
						String title = xpp0.getAttributeValue(null, "title");
						Integer id = Integer.parseInt(xpp0.getAttributeValue(null, "id"));
						m_infernalists.put(id, title);
					}
					if (tagname0.equalsIgnoreCase("religion")) {
						String title = xpp0.getAttributeValue(null, "title");
						Integer id = Integer.parseInt(xpp0.getAttributeValue(null, "id"));
						m_religions.put(id, title);
					}
					break;

				default:
					break;
				}
				//move on to next iteration
				eventType0 = xpp0.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
